package MyCustomTable;

import MyUtil.DataBaseUtil;
import MyUtil.JScrollUtil;
import MyUtil.MyPanel;
import MyUtil.ResultSetTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.*;

public class Pay_List_ResultSet extends JFrame {
    private JScrollPane jScrollPane;
    private JScrollPane jScrollPane1;
    private JButton jb;
    String sql1 = "delete from PAY_LIST where PID = ?";
    String sql2 = "update book set bnumber = bnumber + ? where bid = ?";
    ResultSet rs;

    public Pay_List_ResultSet(String sql) throws SQLException {

        super("销售列表总查询");
        long number = 0;
        this.setBounds(300, 300, 700, 600);
        JPanel jp = new MyPanel("/picture/3.jpg");
        this.setContentPane(jp);
        jp.setLayout(new FlowLayout());
        rs = DataBaseUtil.produceResultSet(sql);
        //jScrollPane = JScrollUtil.getCustomJScrollPane(rs);
        ResultSetTableModel rstm = new ResultSetTableModel(rs);
        JTable jTable = new JTable(rstm);
        jScrollPane = new JScrollPane(jTable);
        jp.add(jScrollPane);
        jb = new JButton("退订");
        jp.add(jb);
        while (rs.next()) {
            number += rs.getFloat("Pprice");
        }
        JLabel j1 = new JLabel("总出售额为" + String.valueOf(number));
        jp.add(j1);
        this.setVisible(true);

        jb.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = jTable.getSelectedRow();
                if (row == -1) {
                    JOptionPane.showMessageDialog(Pay_List_ResultSet.this, "请选中一行订单信息！");
                    return;
                }
                int result = JOptionPane.showConfirmDialog(Pay_List_ResultSet.this, "确定要删除该货物吗？", "删除货物", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {

                    try {
                        DataBaseUtil.changeResultSet(sql1,Integer.parseInt(String.valueOf(jTable.getValueAt(jTable.getSelectedRow(), 0))));

                    } catch (SQLException throwables) {
                        System.out.println("没获取到PID");
                    }
                    String BID = String.valueOf(jTable.getValueAt(jTable.getSelectedRow(),2));
                    int Bnumber = Integer.parseInt(String.valueOf(jTable.getValueAt(jTable.getSelectedRow(),3)));
                    try {
                        DataBaseUtil.changeResultSet(sql2,Bnumber,BID);
                    } catch (SQLException throwables) {
                        System.out.println("数据库修改失败");
                    }
                    JOptionPane.showMessageDialog(Pay_List_ResultSet.this, "删除订单成功！");
                    jp.removeAll();
                    try {
                        jScrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    jp.add(jScrollPane);
                    jp.add(jb);
                    jp.add(j1);
                    jp.setVisible(false);
                    jp.setVisible(true);

                }
            }
        });
    }

}
